//
//  UIAlertController+TS.swift
//  TSKit
//
//  Created by chenyusen on 2018/3/16.
//

import Foundation

public extension UIAlertController {
    typealias Action = (() -> ())
    
    

    /// 弹出一个系统alert框
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - message: 内容
    ///   - actionTitle: 执行标题
    ///   - action: 执行action
    ///   - cancelTitle: 取消标题
    ///   - cancelAction: 取消action
    static func show(title: String? = nil,
                     message: String? = nil,
                     actionTitle: String? = nil,
                     action:(Action)? = nil,
                     cancelTitle: String? = nil,
                     cancelAction:(Action)? = nil) {
        
        let alertController = UIAlertController(title: title,
                                                message: message,
                                                preferredStyle: .alert)
        
        if let cancelTitle = cancelTitle {
            let defaultCancelAction = UIAlertAction(title: cancelTitle, style: .cancel) { (_) in
                cancelAction?()
                alertController.dismiss(animated: true, completion: nil)
            }
            alertController.addAction(defaultCancelAction)
        }
        
        if let actionTitle = actionTitle, let action = action {
            let alertAction = UIAlertAction(title: actionTitle, style: .default) { (_) in
                action()
            }
            alertController.addAction(alertAction)
        }
        
        UIApplication.shared.topViewController()?.present(alertController, animated: true)
        
    }
}
